<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\user;

use app\api\model\dao\Note;
use app\api\model\dao\Signin;
use app\api\model\dao\UserAccount;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\UserCurrency;

use app\api\model\data\note\NoteData;
use app\library\Common;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;

class SigninIndex extends PageBase
{

    private $objNote;
    private $objNoteDate;
    private $objUserAccount;
    private $objNotePraise;
    private $objNoteReply;
    private $objUserCurrency;
    private $objSignin;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteDate = new NoteData();
        $this->objUserAccount = new UserAccount();
        $this->objNotePraise = new NotePraise();
        $this->objNoteReply = new NoteReply();
        $this->objUserCurrency = new UserCurrency();
        $this->objSignin = new Signin();
    }

    public function myExcute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        $user_account = $this->objUserAccount->getUserAccountByUserids([$this->user_id]);
        if (false === $user_account) {
            Log::error(__METHOD__ . ' get user account fail.[userid:' . $this->user_id . ']');
            throw new Exception('get user account fail.', Error::ERR_SYS);
        }
        $user_account = $this->modelToArray($user_account);
        $user_account = !empty($user_account[0]) ? $user_account[0] : [];

        $this->data['user'] = $this->getUser($user_account);

        $this->data['task_new'] = $this->getTaskNew($user_account, $available_bean_task_new);
        $this->data['task_new'] = array_values($this->data['task_new']);

        $this->data['task_daily'] = $this->getTaskDaily($this->user_id, $available_bean_task_daily);
        $this->data['task_daily'] = array_values($this->data['task_daily']);

        $this->data['available_bean'] = $available_bean_task_new + $available_bean_task_daily;
        $this->data['available_bean'] = strval($this->data['available_bean']);

    }

    /**
     * 获取用户信息
     *
     * @return array
     */
    public function getUser($user_account)
    {
        $data = [
            'level' => '0',
            'photo' => '',
            'bean' => '0',
            'signin_day' => '0',
            'is_signin' => '0',
        ];

        if (empty($user_account)) {
            return $data;
        }

        $user_bean = 0;
        $userid = $user_account['id'];
        $user_currency = $this->objUserCurrency->getCurrencyByUserid($userid, Flag::CURENCY_TYPE_INTEGRAL);
        if (false === $user_currency) {
            Log::error(__METHOD__ . ' get user currency fail.[userid:' . $userid . ']');
        } else {
            $user_currency = $this->modelToArray($user_currency);
            $user_bean = $user_currency['amount'];
            $user_account['level'] = Common::conversionLevel($user_bean);
        }

        // 获取连续签到天数
        $sign_ret = $this->objSignin->getSigninByUserid($userid);
        if (false === $sign_ret) {
            throw new Exception('get signin fail...[userid: ' . $userid . ']', Error::ERR_SYS);
        }

        $sign_cnt = 0;
        $is_signin = 0;
        $f = count($sign_ret) - 1;
        if ($f >= 0 && ($sign_ret[$f]['date'] >= (date('Ymd') - 1))) {
            if ($sign_ret[$f]['date'] == date('Ymd')) {
                $is_signin = 1;
            }
            for ($i = $f; $i >= 0; $i--) {
                if (($f - $i) == ($sign_ret[$f]['date'] - $sign_ret[$i]['date'])) {
                    $sign_cnt++;
                } else {
                    break;
                }
            }
        }
        $data['level'] = $user_account['level'];
        $data['photo'] = $user_account['avatar'];
        $data['bean'] = strval($user_bean);
        $data['signin_day '] = strval($sign_cnt); // 连续签到次数
        $data['is_signin '] = strval($is_signin); // 今天是否已经打卡
        return $data;
    }

    /**
     * 获取新手任务
     *
     * @return string
     */
    public function getTaskNew($user_account, &$available_bean = 0)
    {

        $data = [];
        $task = Flag::$task_new;

        if (empty($user_account)) {
            return $task;
        }

        $userid = $user_account['id'];
        // 完善资料
        unset($user_account['id']);
        $account_finish_cnt = 0;
        $account_total_cnt = count($user_account);
        foreach ($user_account as $val) {
            if ($val) {
                $account_finish_cnt++;
            }
        }
        $account_val = ceil($account_finish_cnt * 100 / $account_total_cnt) . '%';
        $is_complete_account = $account_finish_cnt == $account_total_cnt ? 1 : 0;
        // 发帖数
        $note_cnt = $this->objNoteDate->mineNoteCount($userid);
        if ($note_cnt > Flag::CONST_TASK_NEW_NOTE_COUNT) {
            $note_cnt = Flag::CONST_TASK_NEW_NOTE_COUNT;
        }
        $note_val = $note_cnt . '/' . Flag::CONST_TASK_NEW_NOTE_COUNT;
        $is_complete_note = $note_cnt >= Flag::CONST_TASK_NEW_NOTE_COUNT ? 1 : 0;
        // 回帖数
        $note_reply_cnt = $this->objNoteReply->getNoteReplyCountByUserid($userid);
        if ($note_reply_cnt > Flag::CONST_TASK_NEW_NOTE_REPLY_COUNT) {
            $note_reply_cnt = Flag::CONST_TASK_NEW_NOTE_REPLY_COUNT;
        }
        $note_reply_val = $note_reply_cnt . '/' . Flag::CONST_TASK_NEW_NOTE_REPLY_COUNT;
        $is_complete_note_reply = $note_reply_cnt >= Flag::CONST_TASK_NEW_NOTE_REPLY_COUNT ? 1 : 0;
        foreach ($task as $val) {
            if (1 == $val['id']) {
                $val['rate'] = $account_val;
                $val['is_complete'] = $is_complete_account;
                if (!$is_complete_account) {
                    $available_bean += $val['money'];
                }
            } else if (2 == $val['id']) {
                $val['rate'] = $note_val;
                $val['is_complete'] = $is_complete_note;
                if (!$is_complete_note) {
                    $available_bean += $val['money'];
                }
            } else if (3 == $val['id']) {
                $val['rate'] = $note_reply_val;
                $val['is_complete'] = $is_complete_note_reply;
                if (!$is_complete_note_reply) {
                    $available_bean += $val['money'];
                }
            }
            $data[] = $val;
        }
        return $data;
    }

    /**
     * 获取日常任务
     *
     * @return array
     */
    public function getTaskDaily($userid, &$available_bean = 0)
    {
        $data = [];
        $task = Flag::$task_daily;
        $date = date('Ymd');

        if (empty($userid)) {
            return $task;
        }

        // 发帖数
        $note_cnt = $this->objNoteDate->mineNoteCount($userid, $date);
        if ($note_cnt > Flag::CONST_TASK_DAILY_NOTE_COUNT) {
            $note_cnt = Flag::CONST_TASK_DAILY_NOTE_COUNT;
        }
        $note_val = $note_cnt . '/' . Flag::CONST_TASK_DAILY_NOTE_COUNT;
        $is_complete_note = $note_cnt >= Flag::CONST_TASK_DAILY_NOTE_COUNT ? 1 : 0;
        // 回帖数
        $note_reply_cnt = $this->objNoteReply->getNoteReplyCountByUserid($userid, $date);
        if ($note_reply_cnt > Flag::CONST_TASK_DAILY_NOTE_REPLY_COUNT) {
            $note_reply_cnt = Flag::CONST_TASK_DAILY_NOTE_REPLY_COUNT;
        }
        $note_reply_val = $note_reply_cnt . '/' . Flag::CONST_TASK_DAILY_NOTE_REPLY_COUNT;
        $is_complete_note_reply = $note_reply_cnt >= Flag::CONST_TASK_DAILY_NOTE_REPLY_COUNT ? 1 : 0;
        foreach ($task as $val) {
            if (100 == $val['id']) {
                $val['rate'] = $note_val;
                $val['is_complete'] = $is_complete_note;
                if (!$is_complete_note) {
                    $available_bean += $val['money'];
                }
            } else if (101 == $val['id']) {
                $val['rate'] = $note_reply_val;
                $val['is_complete'] = $is_complete_note_reply;
                if (!$is_complete_note_reply) {
                    $available_bean += $val['money'];
                }
            }
            $data[] = $val;
        }
        return $data;
    }
}
